/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 * Description: GPU/NPU send/recv notify(sync) memory manager
 */
#ifndef HCCL_TYPICAL_SYNC_MEM_H
#define HCCL_TYPICAL_SYNC_MEM_H

#include <mutex>
#include <map>
#include "hccl_common.h"
#include "network/hccp.h"
#include "mem_device.h"

namespace hccl {
using HcclRtSignal = void *;

class TypicalSyncMem {
public:
    static TypicalSyncMem &GetInstance();

    // Alloc and free notify memory for sync
    HcclResult AllocSyncMem(int32_t **ptr);
    HcclResult FreeSyncMem(int32_t *ptr);
    HcclResult GetNotifyHandle(u64 notifyVa, HcclRtNotify &notifyHandle);
    HcclResult GetNotifySrcMem(struct mr_info &mrInfo);

private:
    TypicalSyncMem();
    ~TypicalSyncMem();
    // Delete copy and move constructors and assign operators
    TypicalSyncMem(TypicalSyncMem const&) = delete;             // Copy construct
    TypicalSyncMem(TypicalSyncMem&&) = delete;                  // Move construct
    TypicalSyncMem& operator=(TypicalSyncMem const&) = delete;  // Copy assign
    TypicalSyncMem& operator=(TypicalSyncMem &&) = delete;      // Move assign

    HcclResult InitNotifySrcMem();
    HcclResult DeInitNotifySrcMem();
    HcclResult CreateEmptyNotify(HcclRtNotify &notifyHandle);
    HcclResult DestroyNotify(HcclRtNotify notifyHandle);
    HcclResult FreeAllSyncMem();

    RdmaHandle rdmaHandle_ = nullptr;
    struct mr_info notifySrcMrInfo_;
    MrHandle notifySrcMrHandle_;
    std::mutex syncMemMapMutex_;
    DeviceMem srcDevMem_;
    std::map<u64, HcclRtSignal> syncMemMap_;                     // notifyVa - notify map
};
}  // namespace hccl
#endif  // HCCL_TYPICAL_WINDOW_MEM_H